﻿using HalconDotNet;
using Halcon二次开发.Model.储存每个Controller的单例对象;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Halcon二次开发.Model.工具区域的扩展类;
using Halcon二次开发.Controller.画线工具交互;
using Halcon二次开发.Controller.数据加载与保存;
using Halcon二次开发.Model.储放所有单例.储存每个单例界面;
using System.IO;
using Halcon二次开发.Model;
using Halcon二次开发.Model.自定义控件;

namespace Halcon二次开发.View.找圆_线_矩形
{
    public partial class H_FindLineToolForm : Form
    {
        public FindLineToolController controller;

        HwindowFlowNodeUrseControl HwindowFlowNode;
        public H_FindLineToolForm(HwindowFlowNodeUrseControl hwindowFlowNode)
        {
            //$@"Data\工程文件\"
            InitializeComponent();
            this.HwindowFlowNode = hwindowFlowNode; 
            controller = new FindLineToolController(hwindowFlowNode, this, $"Data\\工程文件\\{hwindowFlowNode.projectName}\\{hwindowFlowNode.name}任务文件\\找线工具校验数据",this.numericUpDown1,this.numericUpDown2,this.numericUpDown3,this.numericUpDown4);
            comboBox1.SelectedIndex = 0;
            this.numericUpDown8.Value = 30;
            this.numericUpDown9.Value = 1;
            this.textBox3.Text = "green";
            this.textBox2.Text = "green";
            this.textBox1.Text = "green";
        }
        /// <summary>
        /// 反射调用的方法，请不要修改
        /// </summary>
        /// <param name="path">Json文件的路径</param>
        /// <param name="addres">节点</param>
        public void SetJsonPaths(string path, string addres)
        {
            string paths = path + "\\" + "界面数据\\" + addres + "\\" + addres + "数据.json";
            //调用存储方法
            new LoadData().LoadDatas(paths, this);
        }
        /// <summary>
        /// 绘制区域按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            try
            {
                controller.DrawLine(numericUpDown1, numericUpDown2, numericUpDown3, numericUpDown4, numericUpDown8, numericUpDown9, numericUpDown6, numericUpDown5, textBox1, textBox2, textBox3);
            }
            catch(Exception ex)
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + ":" + ex.Message + "\r\n");
            }
         }
        /// <summary>
        /// 抓边
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            try
            {
                controller.Bound_Line();
            }
            catch (Exception ex)
            {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + ":" + ex.Message + "\r\n");
            }
        }

        private void H_FindLineToolForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            //保存模板数据
            controller.SetModelPaths();
            this.Hide();
            e.Cancel = true;
            controller.ClearObject();
        }
        private void textBox1_Click(object sender, EventArgs e)
        {
            controller.OpenColorDir(sender,e);
        }
        /// <summary>
        /// x移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            controller.Move_X(Convert.ToDouble(numericUpDown1.Value), Convert.ToDouble(numericUpDown2.Value),
                Convert.ToDouble(numericUpDown3.Value), Convert.ToDouble(numericUpDown4.Value));
        }
        /// <summary>
        /// 高度宽度变化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void numericUpDown6_ValueChanged(object sender, EventArgs e)
        {
            controller.PushHeightWidth(Convert.ToDouble(numericUpDown6.Value), Convert.ToDouble(numericUpDown5.Value));
        }

        /// <summary>
        /// 对比度平滑度
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void numericUpDown8_ValueChanged(object sender, EventArgs e)
        {
            controller.PushThresholdSigma(Convert.ToDouble(numericUpDown8.Value), Convert.ToDouble(numericUpDown9.Value));
        }
        /// <summary>
        /// 颜色控件更改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            controller.PushColor(textBox1.Text,textBox2.Text,textBox3.Text);
        }

        /// <summary>
        /// 工具单次运行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            try
            {
              
                controller.Tool_Run();
            }
            catch(Exception ex)
           {
                (FormDb.Db["Form1"] as Form1).textBox1.AppendText(this.Name + ":" + ex.Message + "\r\n");
            }
        }
        /// <summary>
        /// 卡尺数量更改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void numericUpDown4_ValueChanged(object sender, EventArgs e)
        {
            controller.UpdataCaliperNumber(int.Parse(numericUpDown7.Value.ToString()));
        }

        /// <summary>
        /// 反射调用该方法更新窗体句柄
        /// </summary>
        /// <param name="hwindowFlowNode"></param>
        public void PushHWindon(HwindowFlowNodeUrseControl hwindowFlowNode)
        {
            this.HwindowFlowNode = hwindowFlowNode;
            controller.HwindowFlowNode = hwindowFlowNode;
            controller.hw = hwindowFlowNode.hSmartWindowControl1;
        }
    }
}
